#!/bin/bash

#Desc:install redis by source
#Author:zhang huan 
#Create:2020-09-15
#Update:
#Usage:

#源码包路径
srcPath=$1
#安装路径
installPath=/usr/local/redis
servicePath=/usr/lib/systemd/system/redis.service
#创建解压路径并解压
mkdir redisSource
tar -zxf $srcPath -C redisSource
#进入解压目录
cd redisSource/*/
#安装
make PREFIX=$installPath install
mkdir $installPath/conf
cp redis.conf  $installPath/conf/redis.conf

#设置使其启动后后台运行
sed -ri "s/^daemonize.*/daemonize yes/" redis.conf

#创建redis 启动脚本文件：
echo '[Unit]' > $servicePath
echo 'Description=Redis persistent key-value database' >> $servicePath
echo 'After=network.target' >> $servicePath
echo '' >> $servicePath
echo '[Service]' >> $servicePath
echo 'Type=forking' >> $servicePath
echo "ExecStart=${installPath}/bin/redis-server ${installPath}/conf/redis.conf" >> $servicePath
echo 'Restart=always' >> $servicePath
echo '' >> $servicePath
echo '[Install]' >> $servicePath
echo 'WantedBy=multi-user.target' >> $servicePath

#重载服务，设置开机启动，并启动redis
systemctl daemon-reload && systemctl enable redis && systemctl start redis